var page = sm("do_Page");
var app = sm("do_App");
var storage = sm("do_InitData");
var open = require("open");
var nf = sm("do_Notification");
var rootview = ui("$");
var buttonA = mm("do_Animation", "BUTTONTOUCHDOWNS", "app");

//遮罩显示
var animMaskShow = mm("do_Animator");
var propsMS = {bgColor:"00000077"};
animMaskShow.append(300,propsMS,"EaseOut");
//面板显示
var animPanelShow = mm("do_Animator");
var propsPS = {y:250};
animPanelShow.append(300,propsPS,"Linear");
//遮罩隐藏
var animMaskHide = mm("do_Animator");
var propsMH = {bgColor:"00000000"};
animMaskHide.append(300,propsMH,"EaseIn");
//面板隐藏
var animPanelHide = mm("do_Animator");
var propsPH = {y:960};
animPanelHide.append(300,propsPH,"EaseIn");
var param = {};
var stockNum = 0;
//遮罩事件
var maskbg = ui("bgmask");
maskbg.on("touch","",300,function(){
	maskbg.animate(animMaskHide,function(){
		maskbg.visible = false;
	});
	bodyp.animate(animPanelHide);
});
//防止穿透
var bodyp = ui("pbody");
bodyp.on("touch","",300,function(){
	
});
//确定  
var surebtn = ui("do_Button_sure");
surebtn.on("touch","",300,function(){
	//获取规格ID 数量,
	maskbg.animate(animMaskHide,function(){
		maskbg.visible = false;
	});
	bodyp.animate(animPanelHide);
	if(ui("count").text > stockNum){
		nf.toast("库存不足");
	}else{
		param.quantity = ui("count").text;
		page.fire("refreshSpec",param);
		page.fire("refreshSpecText",param.specText+" x "+param.quantity);
	}
});
//zdy
page.on("productSpec",function(data){
	maskbg.visible = data;
	maskbg.animate(animMaskShow);
	bodyp.animate(animPanelShow);
});

var specList = ui("specList");
var specData = mm("do_ListData");
page.on("productSpecList",function(data){
	
	specData.removeAll();
	specData.addData(data);
	specList.bindItems(specData);
	specList.refreshItems();
	//deviceone.print(JSON.stringify(param));
	if(!param.specId){
		init(data); //没有参数时初始化
	}
});

function init(data){
	//默认选中第一个规格信息
	var a0 = data[0];
	ui("do_ImageView_1").source = a0.specLogo;
	ui("labelSelected").text = a0.color +a0.specName;
	ui("do_Label_price").text = "价格￥"+ a0.price;
	ui("do_Label_stock").text = "库存"+a0.stock+"件";;
	
	//设置选中
	param.price = a0.price;
	var d0 = specData.getOne(0);
	param.specId = d0.id;
	stockNum = d0.stock;
	param.specText = d0.color + d0.specName;
	param.img = d0.specLogo;
	d0.layoutBorder = "FF5400FF,1,4";
	d0.fontColor = "FF5400FF";
	specData.updateOne(0, d0);
	specList.refreshItems();
}

page.on("selectSpec",function(index){
	for (var i = 0; i < specData.getCount(); i++) {
		var d = specData.getOne(i);
		if (index == i){
			stockNum = d.stock;
			ui("do_ImageView_1").source = d.specLogo;
			ui("labelSelected").text = d.color + d.specName;
			ui("do_Label_price").text = "价格￥"+d.price;
			ui("do_Label_stock").text = "库存"+d.stock+"件";
			d.layoutBorder = "FF5400FF,1,4";
			d.fontColor = "FF5400FF";
			param.specId = d.id;
			param.price = d.price;
			param.markPrice = d.marketPrice;
			param.img = d.specLogo;
			param.specText = d.color + d.specName;
		}else{
			d.layoutBorder = "c4c9d0FF,1,4";
			d.fontColor = "313131FF";
		}
		specData.updateOne(i, d);
	}
	specList.refreshItems();
});
/*加减*/
var count = ui("count");
var plus = ui("plus");
var subtract = ui("subtract");
plus.on("touch", function() {
	var c = count.text;
	if(c>=1){
		subtract.fontColor = "313131FF";
	}
	count.text = (c * 1) + 1;
});
subtract.on("touch", function() {
	var c = count.text;
	c = c - 1;
	if (c <= 1){
		c = 1;
		subtract.fontColor = "C0C0C0FF";
	}
	count.text = c;
});